package fr.ubordeaux1.incia;


import ij.IJ;

import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Double;

import mmcorej.CMMCore;

import org.micromanager.MMStudioMainFrame;
import org.micromanager.api.ScriptInterface;
import org.micromanager.utils.MMScriptException;

public class InitializeStage {
	
	//DesktopScan desktopScan;
	
	private String xystage_;

	private Double limitAxe;

	private Double blankPosition;

	public BladeScannerPlugin plugin_;
	
	private static CMMCore mmc;
	private static MMStudioMainFrame app_;
	
	
	public InitializeStage(ScriptInterface app, BladeScannerPlugin plugin) {
		
	      app_ = (MMStudioMainFrame) app;
	      plugin_ = plugin;
	      mmc = app_.getMMCore();
	      xystage_ = mmc.getXYStageDevice();
	     // cameraBF = mmc.getCameraDevice();
		
	}

	void initializeStage() {
		
		
		
		//Determined extremity 
		home(xystage_);
		waitForDevice(xystage_);
		
		//record the stage limits
		limitAxe = new Double();
		blankPosition =new Double();
		
		limitAxe.x = getXYStagePosition().x;
		//limaxX = getXYStagePosition().x;
		blankPosition.y = getXYStagePosition().y;

		//Determined the coordinates of blank
		blankPosition.x = limitAxe.x/2;

		//correct the limaxY
		limitAxe.y = blankPosition.y - 1200;

		//Determined the origin
		//setXYPosition(xystage,-100000,-100000); 
		setXYPosition(xystage_,0,0);
		waitForDevice(xystage_); 
		
		//IJ.error("limaxX = "+limaxX+" \n"+"limaxY = "+limaxY+" \n"+"blancY = "+blancY+" \n"+"xystage_ = "+xystage_+" \n");
		//IJ.error(imgScanX+" \n"+imgScanY+" \n");
		
		
	}
	
/////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////	Functions   /////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
	
	//Micromanager error stuff--------------------------
	public static void waitForDevice(String xystage){
	
		try {
			mmc.waitForDevice(xystage);
		} catch (Exception e) {
			
			IJ.error("waitForDevice(xystage) fonction have a problem \n"+" error : "+e);
		}	
	}
	
	public static void setXYPosition(String stage,double x,double y){
		
		try {
			mmc.setXYPosition(stage ,x,y);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			IJ.error("setXYPosition fonction have a problem \n"+" error : "+e);
		}
		
	}
	public static void home(String stage){
		try {
			mmc.home(stage);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			IJ.error("home fonction have a problem \n"+" error : "+e);
		}
	}

	public static Point2D.Double getXYStagePosition() {
		
		Double xyPosition = null; 
		try {
			xyPosition = app_.getXYStagePosition();
		} catch (MMScriptException e) {
			// TODO Auto-generated catch block
			IJ.error("getXYStagePosition fonction have a problem \n"+" error : "+e);
		}
		return xyPosition;
	}
	
	public String getProperty(String typeProp, String valueProp){
		String propReturn= null;
		try {
			propReturn = mmc.getProperty(typeProp, valueProp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			IJ.error("getProperty fonction have a problem \n"+" error : "+e);
		}
		
		return propReturn;
	}
	
/////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////    Getter and Setter   /////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

	protected String getXystage_() {
		return xystage_;
	}

	protected void setXystage_(String xystage_) {
		this.xystage_ = xystage_;
	}

	protected Double getLimitAxe() {
		return limitAxe;
	}

	protected void setLimitAxe(Double limitAxe) {
		this.limitAxe = limitAxe;
	}

	protected Double getBlankPosition() {
		return blankPosition;
	}

	protected void setBlankPosition(Double blankPosition) {
		this.blankPosition = blankPosition;
	}

}
